/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.user.api.feign.RippleRemoteUserService;
import com.pig4cloud.pigx.user.dto.UserApiIpLockDTO;
import com.pig4cloud.pigx.user.entity.UserApiIp;
import com.pig4cloud.pigx.user.service.UserApiIpService;
import com.pig4cloud.pigx.user.vo.UserApiIpCacheVO;
import com.pig4cloud.pigx.user.vo.UserApiIpVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 用户apiKey ip
 *
 * @since 2022-10-28 15:47:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userapiip")
@Api(value = "userapiip", tags = "用户apiKey ip管理")
public class UserApiIpController {

	private final UserApiIpService userApiIpService;

	/**
	 * 分页查询
	 *
	 * @param page      分页条件
	 * @param condition 查询条件
	 * @return {@link R< Page< UserApiIpVO>>}
	 * @since 2022/10/28 17:41
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	// @PreAuthorize("@pms.hasPermission('user_api_ip_view')")
	public R<Page<UserApiIpVO>> page(Page<UserApiIp> page, UserApiIp condition) {
		if (Objects.isNull(condition.getApiKeyId())) {
			throw new RuntimeException("参数异常，apiKeyId为空");
		}

		return R.ok(userApiIpService.page(page, condition));
	}

	/**
	 * 人工封禁
	 *
	 * @param dto dto
	 * @return {@link R< Boolean>}
	 * @since 2022/10/28 17:41
	 */
	@ApiOperation(value = "人工封禁", notes = "人工封禁")
	@PostMapping("/lock/manual")
	// @PreAuthorize("@pms.hasPermission('user_api_ip_edit')")
	public R<Boolean> lockManual(@Validated @RequestBody UserApiIpLockDTO dto) {
		userApiIpService.lockManual(dto);

		return R.ok();
	}

	/**
	 * 人工解封
	 *
	 * @param dto dto
	 * @return {@link R< Boolean>}
	 * @since 2022/10/28 17:41
	 */
	@ApiOperation(value = "人工解封", notes = "人工解封")
	@PostMapping("/unlock/manual")
	// @PreAuthorize("@pms.hasPermission('user_api_ip_edit')")
	public R<Boolean> unlockManual(@Validated @RequestBody UserApiIpLockDTO dto) {
		userApiIpService.unlockManual(dto);

		return R.ok();
	}

	/**
	 * 根据AccessKey查询相关IP信息
	 *
	 * @param apiKeyId  apiKeyId
	 * @param accessKey 访问密钥
	 * @return {@link R< List< UserApiIpCacheVO>>}
	 * @since 2022/11/9 10:53
	 */
	@Inner
	@GetMapping(RippleRemoteUserService.GET_API_KEY_IP_CACHE)
	R<List<UserApiIpCacheVO>> getApiKeyIpCache(@RequestParam Long apiKeyId,
											   @RequestParam String accessKey) {
		return R.ok(userApiIpService.getCache(apiKeyId, accessKey));
	}
}
